package cn.iocoder.yudao.module.hs.controller.admin.shiporder;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.hs.controller.admin.entryorder.vo.EntryOrderTrayRespVO;
import cn.iocoder.yudao.module.hs.controller.admin.shiporder.vo.ManualShipOrderSaveReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.shiporder.vo.ShipOrderPageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.shiporder.vo.ShipOrderRespVO;
import cn.iocoder.yudao.module.hs.controller.admin.shiporder.vo.ShipOrderTrayRespVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.shiporder.ShipOrderDO;
import cn.iocoder.yudao.module.hs.service.shiporder.ShipOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 出库单")
@RestController
@RequestMapping("/hs/ship-order")
@Validated
public class ShipOrderController {

    @Resource
    private ShipOrderService shipOrderService;

    @DeleteMapping("/delete")
    @Operation(summary = "删除出库单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hs:ship-order:delete')")
    public CommonResult<Boolean> deleteShipOrder(@RequestParam("id") Long id) {
        shipOrderService.deleteShipOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得出库单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hs:ship-order:query')")
    public CommonResult<ShipOrderRespVO> getShipOrder(@RequestParam("id") Long id) {
        ShipOrderDO shipOrder = shipOrderService.getShipOrder(id);
        return success(BeanUtils.toBean(shipOrder, ShipOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得出库单分页")
    @PreAuthorize("@ss.hasPermission('hs:ship-order:query')")
    public CommonResult<PageResult<ShipOrderRespVO>> getShipOrderPage(@Valid ShipOrderPageReqVO pageReqVO) {
        return success(shipOrderService.getShipOrderPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出出库单 Excel")
    @PreAuthorize("@ss.hasPermission('hs:ship-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportShipOrderExcel(@Valid ShipOrderPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        // 导出 Excel
        ExcelUtils.write(response, "出库单.xls", "数据", ShipOrderRespVO.class, shipOrderService.getShipOrderPage(pageReqVO).getList());
    }

    @PostMapping("/ship")
    @Operation(summary = "更新出库单状态为出库中")
    @PreAuthorize("@ss.hasPermission('hs:ship-order:update')")
    public CommonResult<Boolean> ship(@RequestParam("shipOrderId") Long shipOrderId) {
        shipOrderService.ship(shipOrderId);
        return success(true);
    }

    @PostMapping("/create-manual")
    @Operation(summary = "手动创建三方仓出库单")
    @PreAuthorize("@ss.hasPermission('hs:ship-order:create-manual')")
    public CommonResult<Long> createManualShipOrder(@Valid @RequestBody ManualShipOrderSaveReqVO saveReqVO) {
        return success(shipOrderService.createManualShipOrder(saveReqVO));
    }

    @GetMapping("/get-tray-list")
    @Operation(summary = "获得出库单中托盘列表")
    @Parameter(name = "shipOrderId", description = "收货单对应的出库单ID", required = true)
    @Parameter(name = "warehouseId", description = "仓库ID", required = true)
    @PreAuthorize("@ss.hasPermission('hs:ship-order:query')")
    public CommonResult<List<ShipOrderTrayRespVO>> getTrayList(@RequestParam("shipOrderId") Long shipOrderId, @RequestParam("warehouseId") Long warehouseId) {
        return success(shipOrderService.getTrayList(shipOrderId, warehouseId));
    }
}